<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport"
	content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes" />
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>注册</title>
<link rel="stylesheet" type="text/css" href="css/register.css">
</head>
<body>
	<header>
		<a class="backButton" href=""></a>
		<h1>智能储物柜</h1>
	</header>
	<div class="login_box">
		<p class="form-p">
			<input class="ipt phoneipt" type="tel" placeholder="手机号码">
		</p>
		<p class="form-p code-p">
			<input class="ipt codeipt" type="text" placeholder="验证码"><a
				href="#" class="getcodebtn" onclick="getCode(this);">获取验证码</a>
		</p>
		<p class="form-p msg-p">
			<span style="display: none;color: red;" class="msg"></span>
		</p>
		<a href="#" onclick="checkCode(this)" class="nextbtn">注册</a>
	</div>

	<script src="js/jquery-2.1.1.js"></script>
	<script src="js/jsrender.min.js"></script>
	<script>
	 function getQueryString(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
			var r = window.location.search.substr(1).match(reg);
			if (r != null) return unescape(r[2]); return null;
		}
		 
		var openId= getQueryString("id");
		var suffixUrl = "/leo";
		var checkCode = function(e){
			var telnumber = $(".phoneipt").val();
			var type = "regist";
			var code = $(".codeipt").val();
			$(".msg").show();
			if($.trim(telnumber).length != 11){
				$(".msg").html("手机号格式错误");
			}else if($.trim(code).length != 6){
				$(".msg").html("请输入6位验证码");
			}else{
				 $.post(suffixUrl + '/user/register', {
			 			"mobile": telnumber,
			 			"code": code,
			 			"openId": openId,
			 		}).always(
			 				function(data) {
			 					if(data.code == 0){
			 						alert("注册成功");
									window.location.href = "tools.html?userId=" + data.value;								
			 					}else{
			 						alert(data.detail);
			 					}
			 				}, "json").fail(function() {
			 			alert("fail！");
			 			return;
			 		});
			}
		};
		var getCode = function(evt){
			var telnumber = $(".phoneipt").val();
			var type = "regist";
			$(".msg").show();
			if($.trim(telnumber).length != 11){
				$(".msg").html("手机号格式错误");
			}else{
				$.post(suffixUrl + "/user/getCode",{
					"mobile" : telnumber
				},function(data){
					$(".msg").html(data.value);
					if(data.code == 0){
						settime($(evt));
					}
				});
			}
		};
		
		
		var countdown = 59;
		settime = function(val){
			if (countdown == 0) {
				val.attr("onclick","getCode(this);");
				val.html("获取验证码");
				countdown = 59;
				return;
			} else {
				val.attr("onclick", "");
				val.html("重新发送(" + countdown + ")");
				countdown--;
			}
			setTimeout(function() {
				settime(val);
			}, 1000);
		};
	</script>
</body>
</html>